package com.ruoyi.equ.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import com.ruoyi.equ.domain.EquCollect;
import com.ruoyi.equ.service.IEquBasicService;
import com.ruoyi.equ.service.IEquCollectService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备领用Controller
 * 
 * @author luguanqing
 * @date 2020-10-23
 */
@RestController
@RequestMapping("/equ/collect")
public class EquCollectController extends BaseController
{
    @Autowired
    private IEquCollectService equCollectService;

    @Autowired
    private IEquBasicService equBasicService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询设备领用列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EquCollect equCollect)
    {
        startPage();
        List<EquCollect> list = equCollectService.selectEquCollectList(equCollect);
        return getDataTable(list);
    }

    /**
     * 导出设备领用列表
     */
    @Log(title = "设备领用", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EquCollect equCollect)
    {
        List<EquCollect> list = equCollectService.selectEquCollectList(equCollect);
        ExcelUtil<EquCollect> util = new ExcelUtil<EquCollect>(EquCollect.class);
        return util.exportExcel(list, "collect");
    }

    /**
     * 获取设备领用详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo( @PathVariable("id") String id)
    {
        return AjaxResult.success(equCollectService.selectEquCollectById(id));
    }

    /**
     * 根据单号获取设备领用详细信息
     */
    @GetMapping(value = "/getInfoByColNum/{colNum}/{state}")
    public AjaxResult getInfoByColNum( @PathVariable("colNum") String colNum, @PathVariable("state") String state)
    {
        return AjaxResult.success(equCollectService.selectEquCollectByColNum(colNum, state));
    }


    /**
     * 根据单号获取设备领用详细信息
     */
    @GetMapping(value = "/getInfoByCol/{colNum}")
    public AjaxResult getInfoByCol(@PathVariable("colNum") String colNum)
    {
        return AjaxResult.success(equCollectService.selectEquCollectNum(colNum));
    }


    /**
     * 新增设备领用
     */
    @Log(title = "设备领用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquCollect equCollect)
    {
        List<EquCollect> equCollectList = new ArrayList<EquCollect>();
        ////获取数量
        //List<Map<String, String>> equList = equCollect.getEquInformation();
        //if(equList.size() > 0){
        //    for(int i = 0; i < equList.size(); i++){
        //        Integer nums = Integer.valueOf(equList.get(i).get("nums"));
        //        //获取基本信息
        //        EquBasic basicList = equBasicService.selectEquBasicByEquNum(equList.get(i).get("equNum"), "1");
        //        if(basicList.size() >= nums){
        //            for(int j = 0; j < nums; j++){
        //                EquCollect collect = new EquCollect();
        //                collect.setId(IdUtils.fastSimpleUUID());
        //                collect.setEquId(basicList.get(j).getId());
        //                collect.setEquName(basicList.get(j).getEquName());
        //                collect.setEquNum(basicList.get(j).getEquNum());
        //                collect.setColNum(equCollect.getColNum());
        //                collect.setColState("1");
        //                equCollectList.add(collect);
        //                //更新状态
        //                EquBasic equState = new EquBasic();
        //                equState.setId(basicList.get(j).getId());
        //                equState.setState("2");
        //                equBasicService.updateEquBasic(equState);
        //            }
        //        }else{
        //            return AjaxResult.error("库存不足，请重新下单");
        //        }
        //    }
            return toAjax(equCollectService.insertEqus(equCollectList));
        //}else{
        //    return AjaxResult.error("未选择出库设备");
        //}
    }

    /**
     * 修改设备领用
     */
    @Log(title = "设备领用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquCollect equCollect)
    {
        return toAjax(equCollectService.updateEquCollect(equCollect));
    }

    /**
     * 删除设备领用
     */
    @Log(title = "设备领用", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(equCollectService.deleteEquCollectByIds(ids));
    }
}
